﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE
{
    public partial class frmphieu : PRE.Catalog.FrmBase
    {
        DAL.EntitiesDAL.phieu_DAL _dal = new DAL.EntitiesDAL.phieu_DAL();
        BLL.EntitiesBLL.phieuBLL _bll = new BLL.EntitiesBLL.phieuBLL();
        public frmphieu()
        {
            InitializeComponent();
        }
        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {
            DataTable _ma = _dal.Select_max();
            if (_ma != null)
                if (_ma.Rows.Count > 0)
                {
                    string ma = _bll.AddID(_ma.Rows[0]["id"] + "");
                    txtmaphieu.Text = ma;

                    ReadOnlyControl(false);
                    ClearDataBindings();
                    ChangeStatus(false);
                    ResetText();
                    txtngaytaophieu.Focus();
                }

            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            string id = txtmaphieu.Text;

            DialogResult result;
            var ok = _dal.Delete(id);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    MessageBox.Show("Xóa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            //txtmaphieu.Enabled = false;
            ReadOnlyControl(false);
            txtngaytaophieu.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();
            txtmaphieu.Properties.ReadOnly = true;

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }
            base.PerformRefresh();
        }
        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            //if (txttinhtien.Text == null) txttinhtien.Text = "0";
            //txttinhtien.Text = "0";
            //var ten = Proper(txttinhtien.Text);
            //var DVT = Proper(txtDVT.Text);
            //var DG = Proper(txtDG.Text);
            if (IsAdd)
            {
                var o = new DAL.Entities.phieu()
                {
                    MAPHIEU = txtmaphieu.Text,
                    TINHTIEN  = txttinhtien.Text,
                    //NGAYTAOPHIEU = txtngaytaophieu.EditValue + "",
                    NGAYTAOPHIEU = Convert.ToDateTime(txtngaytaophieu.EditValue),
                    MABAN = cbbxmaban.EditValue + ""
                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus(false);
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            if (IsEdit)
            {
                //var _id = grvMain.GetFocusedRowCellValue("MA_SP") + "";

                var o = new DAL.Entities.phieu()
                {
                    MAPHIEU = txtmaphieu.Text,
                    TINHTIEN = txttinhtien.Text,

                    NGAYTAOPHIEU = Convert.ToDateTime(txtngaytaophieu.EditValue),
                    MABAN = cbbxmaban.EditValue + ""
                };

                var oki = _dal.Update(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            base.PerformSave();
        }
        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            cbbxmaban.Text = null;
            
            txttinhtien.Text = "0";
            //txtmaphieu.Text = null;
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txtmaphieu.DataBindings.Clear();
            txtngaytaophieu.DataBindings.Clear();
            cbbxmaban.DataBindings.Clear();
            txttinhtien.DataBindings.Clear();

            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            txtmaphieu.DataBindings.Add("EditValue", _dtb, ".MAPHIEU");
            txtngaytaophieu.DataBindings.Add("EditValue", _dtb, ".NGAYTAOPHIEU");
            cbbxmaban.DataBindings.Add("EditValue", _dtb, ".MABAN");
            txttinhtien.DataBindings.Add("EditValue", _dtb, ".TINHTIEN");
            //
            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            txtmaphieu.Properties.ReadOnly = isReadOnly;
            txtngaytaophieu.Properties.ReadOnly = isReadOnly;
            cbbxmaban.Properties.ReadOnly = isReadOnly;
            txttinhtien.Properties.ReadOnly = isReadOnly;
            dsphieu.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            //ClearDataBindings();
            //ResetText();
            //ChangeStatus(false);
            //ReadOnlyControl(false);

            //txtMaSP.Properties.ReadOnly = false;
            //txtMaSP.ResetText();
            //txtMaSP.Focus();

            //dsSanpham.Enabled = true;

            base.PerformSearch();
        }
        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            _dtb = _dal.Select();

            if (_dtb != null)
            {
                dsphieu.DataSource = _dtb;

            }

            base.LoadData();
        }
        #endregion


        private void frmphieu_Load(object sender, EventArgs e)
        {
            DataTable _dtb1 = _dal.BAN();
            cbbxmaban.Properties.DataSource = _dtb1;
        }

        private void cmdIn_Click(object sender, EventArgs e)
        {
            var stt = gridView1.GetFocusedRowCellValue("MAPHIEU") + "";

            var frm = new Reports.frmbase();
            var xr = new Reports.baocao();

            xr.DataSource = _dal.Select_Report(stt);
            frm.SetReport(xr);
            frm.Show();
           
        }

        private void cmdThanhtien_Click(object sender, EventArgs e)
        {
            var o = new DAL.Entities.phieu()
            {
                MAPHIEU = txtmaphieu.Text,
                TINHTIEN = label9.Text,

                NGAYTAOPHIEU = Convert.ToDateTime(txtngaytaophieu.EditValue),
                MABAN = cbbxmaban.EditValue + ""
            };

            var oki = _dal.Update(o);
            if (oki)
            {
                MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                PerformRefresh();
                ChangeStatus();
            }
            else
            {
                MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                ChangeStatus(false);
                ReadOnlyControl(false);
            }
        }

        private void dsphieu_Click(object sender, EventArgs e)
        {
            string _dem = _dal.sum(txtmaphieu.Text.Trim());
            label9.Text = _dem.ToString();
        }
    }
}
